package com.egdigital.allzic.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;

public class CacheManager {

	private static final long	MAX_SIZE	= 5242880L; // 5MB

	/**
	 * @param context
	 * @param data
	 * @param name
	 * @throws IOException
	 */
	public static void cacheData(Context context, byte[] data, String name) throws IOException {
		File cacheDir = context.getCacheDir();
		long size = getDirSize(cacheDir);
		long newSize = data.length + size;
		if (newSize > MAX_SIZE) {
			cleanDir(cacheDir, newSize - MAX_SIZE);
		}
		File file = new File(cacheDir, name);
		FileOutputStream os = new FileOutputStream(file);
		try {
			os.write(data);
		} finally {
			os.flush();
			os.close();
		}
	}

	/**
	 * @param context
	 * @param name
	 * @return
	 * @throws IOException
	 */
	public static FileInputStream retrieveData(Context context, String name) throws IOException {
		File cacheDir = context.getCacheDir();
		File file = new File(cacheDir, name);
		if (!file.exists()) {
			// Data doesn't exist
			return null;
		}
		byte[] data = new byte[(int) file.length()];
		FileInputStream is = new FileInputStream(file);
		try {
			is.read(data);
		} finally {
			is.close();
		}
		return is;
	}

	/**
	 * @param dir
	 * @param bytes
	 */
	private static void cleanDir(File dir, long bytes) {
		long bytesDeleted = 0;
		File[] files = dir.listFiles();
		for (File file : files) {
			bytesDeleted += file.length();
			file.delete();
			if (bytesDeleted >= bytes) {
				break;
			}
		}
	}

	/**
	 * @param dir
	 * @return
	 */
	private static long getDirSize(File dir) {
		long size = 0;
		File[] files = dir.listFiles();
		for (File file : files) {
			if (file.isFile()) {
				size += file.length();
			}
		}
		return size;
	}
}